<?php
namespace Popshouse\SiteBundle\Entity;
use Popshouse\AdminBundle\Model\Translation;
/**
 * @orm:Entity
 * @orm:Table(name="image")
 * @orm:InheritanceType("SINGLE_TABLE")
 * @orm:DiscriminatorColumn(name="type", type="integer")
 * @orm:DiscriminatorMap({"3" = "Popshouse\SiteBundle\Entity\Link", "2" = "Popshouse\SiteBundle\Entity\Pageimg", "1" = "Popshouse\SiteBundle\Entity\News", "0" = "Popshouse\SiteBundle\Entity\Background"})
 * @orm:HasLifecycleCallbacks
 */
abstract class Image{
	/**
     * @orm:Id
     * @orm:Column(name="id",type="integer")
     * @orm:GeneratedValue(strategy="AUTO")
     */
	protected $id;
	
	/** 
	 * @orm:Column(name="sort",type="integer")
     */
	protected $sort = 0;
	
	protected $strings;
	
	protected $translation;
	
	public $keys = array('title');

	
	/**
	 * @return the $id
	 */
	public function getId() {
		return $this->id;
	}
	
	protected function addKeys(array $keys){
		$this->keys = array_merge($this->keys,$keys);
		//die(print_r($this->keys,true));
	}

	/**
	 * @return the $sort
	 */
	public function getSort() {
		return $this->sort;
	}

	/**
	 * @param field_type $sort
	 */
	public function setSort($sort) {
		$this->sort = $sort;
	}

	/**
	 * @return the $strings[nl]
	 */
	public function getNl() {
		return $this->strings['nl'][$this->keys[0]];
	}

	/**
	 * @param field_type $nl
	 */
	public function setNl($nl) {
		$this->strings['nl'][$this->keys[0]] = $nl;
	}
	
	/**
	 * @return the $strings[en]
	 */
	public function getEn() {
		return $this->strings['en'][$this->keys[0]];
	}

	/**
	 * @param field_type $nl
	 */
	public function setEn($en) {
		$this->strings['en'][$this->keys[0]] = $en;
	}
/**
	 * @return the $strings[fr]
	 */
	public function getFr() {
		return $this->strings['fr'][$this->keys[0]];
	}

	/**
	 * @param field_type $fr
	 */
	public function setFr($fr) {
		$this->strings['fr'][$this->keys[0]] = $fr;
	}
/**
	 * @return the $strings[de]
	 */
	public function getDe() {
		return $this->strings['de'][$this->keys[0]];
	}

	/**
	 * @param field_type $de
	 */
	public function setDe($de) {
		$this->strings['de'][$this->keys[0]] = $de;
	}

	protected function initTranslation(){
		
		if($this->translation === null){
			$this->translation = new Translation($this->getTranslationName());
			$this->translation->load();
		}
	}
	
	public function loadTranslation(){
		if($this->strings === null || !sizeof($this->strings)){
			$this->initTranslation();
			foreach($this->keys as $key){
				foreach($this->translation->getLangs() as $lang){
					
					if(array_key_exists($key.'.'.$this->getId(), $this->translation->strings) && array_key_exists($lang, $this->translation->strings[$key.'.'.$this->getId()]))
						$this->strings[$lang][$key] = $this->translation->strings[$key.'.'.$this->getId()][$lang];
					else
						$this->strings[$lang][$key] = "";
				}
				
			}
		}
	}
	
	/**
	 * @orm:PostPersist 
	 * @orm:PostUpdate
	 */
	public function onChange(){
		$this->initTranslation();
		$this->loadTranslation();
		//die(print_r($this->strings,true));
		foreach($this->keys as $key){
			foreach($this->translation->getLangs() as $lang){
				if(array_key_exists($lang, $this->strings) && array_key_exists($key, $this->strings[$lang]))
					$set = $this->strings[$lang][$key];
				else
					$set = "";
					
				$this->translation->set($key.'.'.$this->getId(),$lang,$set);
			}
		}
		$this->translation->save();
	}

	
	/**
	 * @orm:PreRemove
	 */
	public function onRemove(){
		$this->initTranslation();
		foreach($this->keys as $key)
			$this->translation->removeKey($key.'.'.$this->getId());
			
		$this->translation->save();
		@unlink($this->getImagePath());
		@unlink($this->getImagePath(true));
	}
	
	
	public function getImageUrl($thumb=false){
		$url = "images/photo/". $this->getImageDir() ."/".$this->getId();
		$url .= $thumb ? "t" : "";
		$url .= ".jpg";
		return $url;
	}
	
	protected function getTranslationName(){
		return Translation::imgs;
	}
	
	protected function getImageDir(){
		return "images";
	}
	
	public function getImagePath($thumb=false){
		return PUBLIC_PATH."/".$this->getImageUrl($thumb);
	}
	
	public function getAfbeelding(){}
	public function setAfbeelding($afbeelding){}
	
	
}